#include "buffer.h"
#include <iostream>
#include <string>

using namespace std;

const string videoSource("\\\\ibm134\\Public\\temp\\Osl_clips\\demo_big.avi");

int test6(){
	int overload = 2400;

	int fps = 24;
	int time = 10;
	Buffer buf( time, fps );
	CvCapture* video = cvCaptureFromFile(videoSource);
	IplImage *currImage = NULL;		// a still shot from the video feed
	int num = 0;
	int counter = 0;

	cvNamedWindow("TDD", 1);

	while ( cvGrabFrame(video) && overload > 0 ){   // while feed is alive - grab a frame   
		currImage =  cvRetrieveFrame(video);  // retrieve the grabbed frame
		cvShowImage("TDD", currImage);
		buf.saveImage( currImage );
		counter++;
		if( counter == 100 ) {
			string path(buf.createVideoLog( num ));
			counter = 0;
			num++;
		}
		cvWaitKey( 30 );
		overload--;
	}


	cvReleaseCapture( &video );
	return 0;
}


int test5(){
	int fps = 24;
	int time = 10;
	Buffer buf( time, fps);
	int i;
	CvCapture* video;
	int frames_num;

	frames_num = 200;
	video = cvCaptureFromFile(videoSource);
	for( i=0; i<frames_num; i++ ){
		cvGrabFrame(video);
		buf.saveImage(cvRetrieveFrame(video));
	}
	string path1 (buf.createVideoLog( 0 ));
	cvReleaseCapture( &video );
	Sleep( 1000 );
	video = cvCaptureFromFile("log\\car0.avi");
	for( i=0; i<frames_num; i++ ){
		cvGrabFrame(video);
		if(cvRetrieveFrame(video) == NULL) {
			cvReleaseCapture( &video );
			return 1;
		}
	}
	cvReleaseCapture( &video );


	video = cvCaptureFromFile(videoSource);
	frames_num = 400;
	for( i=0; i<frames_num; i++ ){
		cvGrabFrame(video);
		buf.saveImage(cvRetrieveFrame(video));
	}

	string path2 (buf.createVideoLog( 1 ));
	cvReleaseCapture( &video );
	Sleep( 3000 );
	video = cvCaptureFromFile("log\\car1.avi");
	for( i=0; i<fps*time; i++ ){
		cvGrabFrame(video);
		if(cvRetrieveFrame(video) == NULL) {
			cvReleaseCapture( &video );
			return 1;
		}
	}

	cvReleaseCapture( &video );
	return 0;
}


int test4(){
	CvCapture* video = cvCaptureFromFile(videoSource);
	Buffer buf( 10, 24);

	cvGrabFrame(video);
	buf.saveImage(cvRetrieveFrame(video));

	string path (buf.createVideoLog( 0 ));
	cvReleaseCapture( &video );
	Sleep( 1000 );
	video = cvCaptureFromFile("log\\car0.avi");
	if( !video ) return 1;

	cvReleaseCapture( &video );
	return 0;
}


int test3(){
	Buffer buf( 10, 24);
	string path (buf.createVideoLog( 0 ));
	if( path!="log\\car0.avi" ) return 1;
	return 0;
}


int test2(){
	Buffer buf( 10, 24);
	string path (buf.createVideoLog( 0 ));
	return 0;
}


int test1(){
	Buffer buf( 10, 24);
	char* path = buf.createVideoLog( 0 );
	return 0;
}

void main(){
	if( test1() ) cout<<"Test 1 FAILED!"<<endl;
	else cout<<"Test 1 passed"<<endl;
	if( test2() ) cout<<"Test 2 FAILED!"<<endl;
	else cout<<"Test 2 passed"<<endl;
	if( test3() ) cout<<"Test 3 FAILED!"<<endl;
	else cout<<"Test 3 passed"<<endl;
	if( test4() ) cout<<"Test 4 FAILED!"<<endl;
	else cout<<"Test 4 passed"<<endl;
	if( test5() ) cout<<"Test 5 FAILED!"<<endl;
	else cout<<"Test 5 passed"<<endl;
	if( test6() ) cout<<"Test 6 FAILED!"<<endl;
	else cout<<"Test 6 passed"<<endl;
	cout<<endl<<"Tests ended"<<endl;
}

